﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GamePlay;
using MainGame.GameSound;

namespace MainGame.Menu
{

  public interface ILevelRecapScreen
  {
    event MenuEvent PlayAgain;
    event MenuEvent BackToMain;
  }

  /// <summary>
  /// Mostriamo lo score di fine livello
  /// </summary>
  public class LevelRecapScreen : BaseMenu, ILevelRecapScreen
  {
    Button NewGameBtn, BackToMainBtn;
    Texture2D white_pixel, box;
    float ScaleFactor = 15f;
    SpriteFont largeFont, baveuse;
    Rectangle col1, col2, col3, col4, Roof, Anim;
    
    
    // Moltiplicatore difficolta', moltiplicatori prime sei carte, bonus mazzetto + grande
    float elapsedTime;
    Texture2D colonna, colonnaLost;
    bool win = false, sound = true;

    public LevelRecapScreen(Game game, MenuDepths depth, bool win)
    : base(game, depth)
    {
      this.win = win;
      background = null;     

      bounds = game.GraphicsDevice.Viewport.Bounds;
      bounds = new Rectangle(0, 0, 800, 480);
      game.Services.AddService(typeof(ILevelRecapScreen), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(ILevelRecapScreen));
      base.Dispose(disposing);
    }

    public override void Initialize()
    {
      Anim = new Rectangle(650, 100, 140, 140);

      col1 = new Rectangle(bounds.Width / 2 - 50, 680, 62, 260);
      col2 = new Rectangle(bounds.Width / 2 + 70, 680, 62, 260);
      col3 = new Rectangle(bounds.Width / 2 + 190, 680, 62, 260);
      col4 = new Rectangle(bounds.Width / 2 + 310, 680, 62, 260);
      Roof = new Rectangle(bounds.Width / 2 - 60, -400, 550, 400);

      colonna = Game.Content.Load<Texture2D>(@"Images//Menu//Colonna");
      colonnaLost = Game.Content.Load<Texture2D>(@"Images//Menu//ColonnaLost");

      var viewportBounds = Game.GraphicsDevice.Viewport.Bounds;

      largeFont = Game.Content.Load<SpriteFont>("large_font");
      baveuse = Game.Content.Load<SpriteFont>("baveuse1_font");
      white_pixel = Game.Content.Load<Texture2D>("white_pixel");
      box = Game.Content.Load<Texture2D>(@"Images//Menu//HelpBg2");

      NewGameBtn = new Button(Game, @"Images//Menu//button2", new Rectangle(viewportBounds.Width - 220, 350, 150, 80), Color.Red, "baveuse1_font", Locale.Strings.NewGame.Replace("\\n", "\n"), Color.White, 0.6f);
      AddButton(NewGameBtn, () =>
      {
        if (PlayAgain != null)
          PlayAgain();
      });

      BackToMainBtn = new Button(Game, @"Images//Menu//button2", new Rectangle(70, 350, 150, 80), Color.Blue, "baveuse1_font", Locale.Strings.BackToMain.Replace("\\n", "\n"), Color.White, 0.6f);
      AddButton(BackToMainBtn, () =>
      {
        if (BackToMain != null)
          BackToMain();
      });

      base.Initialize();
    }

    SpriteBatch spriteBatch;
    Matrix screen_transform;
    protected override void LoadContent()
    {
      var content = Game.Content;

      spriteBatch = Game.GetContext().SpriteBatch;
      screen_transform = Game.GetContext().ScreenTransform;

      base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {
      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        if (BackToMain != null)
          BackToMain();
      }

      elapsedTime += gameTime.dt();
      
      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, null, null, null, null, screen_transform);

      if (win)
      {
        spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images\\Menu\\RecapScreen"), new Rectangle(0, 0, 800, 480), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.5f);

        spriteBatch.Draw(colonna, col1, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
        spriteBatch.Draw(colonna, col2, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
        spriteBatch.Draw(colonna, col3, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
        spriteBatch.Draw(colonna, col4, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
      }
      else
      {
        spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images\\Menu\\RecapScreenLost"), new Rectangle(0, 0, 800, 480), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.5f);

        spriteBatch.Draw(colonnaLost, col1, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
        spriteBatch.Draw(colonnaLost, col2, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
        spriteBatch.Draw(colonnaLost, col3, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
        spriteBatch.Draw(colonnaLost, col4, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
      }

      spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Menu//Tetto"), Roof, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.7f);
      
      if (col1.Y > 220)
        col1.Y -= 10;

      if ((col1.Y < 660) && (col2.Y > 220))
          col2.Y -= 10;

      if ((col1.Y < 640) && (col3.Y > 220))
          col3.Y -= 10;

      if ((col1.Y < 620) && (col4.Y > 220))     
          col4.Y -= 10;

      if (col1.Y <= 220)
        if (Roof.Y < -110)
          Roof.Y += 10;          


      if (Roof.Y >= -110)
      {
        if (sound)
        {
          var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
          SoundManager.PlaySound(MenuSounds.Fall);
          sound = false;
        }

        if (win)
        {
          spriteBatch.DrawString(baveuse, Locale.Strings.Won.Replace("\\n", "\n"), new Vector2(bounds.Width / 2 - 327, 97), Color.Black, 0, Vector2.Zero, ScaleFactor, SpriteEffects.None, 0.9f);
          spriteBatch.DrawString(baveuse, Locale.Strings.Won.Replace("\\n", "\n"), new Vector2(bounds.Width / 2 - 330, 100), Color.Blue, 0, Vector2.Zero, ScaleFactor, SpriteEffects.None, 1);
        }
        else
        {
          spriteBatch.DrawString(baveuse, Locale.Strings.Lost.Replace("\\n", "\n"), new Vector2(bounds.Width / 2 - 327, 97), Color.Black, 0, Vector2.Zero, ScaleFactor, SpriteEffects.None, 0.9f);
          spriteBatch.DrawString(baveuse, Locale.Strings.Lost.Replace("\\n", "\n"), new Vector2(bounds.Width / 2 - 330, 100), Color.Red, 0, Vector2.Zero, ScaleFactor, SpriteEffects.None, 1);
        }

        if (ScaleFactor > 2)
          ScaleFactor -= 0.7f;
      }

      spriteBatch.End();
      base.Draw(gameTime);
    }

    public event MenuEvent PlayAgain;
    public event MenuEvent BackToMain;
  }
}
